package cn.wanho.controller;

import cn.wanho.rvo.ResultEnum;
import cn.wanho.rvo.ResultVO;
import cn.wanho.service.user.UserService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 登录功能，将jwt存到 redis中( 修改后，已经交给springsecurity完成 )
     */
//    @PostMapping("/login")
//    public ResultVO login(String username, String password){
//        System.out.println("进入login中");
//        return userService.login(username,password);
//    }

    @PostMapping("/createAdmin")
    public ResultVO createAdmin(String userName,HttpServletRequest request){
        Integer userId = (Integer)request.getAttribute("userId");
        return userService.insertAdmin(userId,userName);
    }

    /**
     * @author jingxu
     * @date 2022-8-5
     *这是个公共接口，用于用户来修改自己的信息
     */
    @PostMapping("/updateuser")
    public ResultVO updateUser(String userName, String userIcon, String personInfo,HttpServletRequest request) throws Exception {
        Integer userId = (Integer)request.getAttribute("userId");
        return userService.updateUserById(Long.valueOf(userId), userName, userIcon, personInfo);
    }

    /**
     * @author jingxu
     * @date 2022-8-5
     * 学生查询个人信息
     */
    @GetMapping("/selectAll")
    public ResultVO selectAll(HttpServletRequest request){
        Integer userId = (Integer)request.getAttribute("userId");
        return userService.selectStudentInformation(Long.valueOf(userId));
    }

    /**
     * 修改密码
     */
    @PostMapping("/updatePass")
    public ResultVO update(HttpServletRequest request,String oldPass,String newPass){
        Integer userId = (Integer)request.getAttribute("userId");
        return userService.updatePassWord(oldPass,newPass,userId);
    }

    /**
     * @author zxy
     * @date 2022/8/5
     */
    //创建老师
    @PostMapping("/createTeacher")
    public ResultVO createTeacher(String userName, String userPass,HttpServletRequest request) {
        return userService.createTeacher(userName,userPass,request);
    }

    /**
     * @author zxy
     * @date 2022/8/5
     */
    //excel添加
    @RequestMapping("/createStudents")
    public ResultVO createStudents(@RequestPart("file")MultipartFile file, HttpServletRequest request) throws IOException {
        return userService.importExcel(file,request);
    }

}
